package com.itheima.algorithm.linkedlist;

import java.util.Iterator;

/**
 * 双向链表（带哨兵）
 */
public class DoubleLinkedListSentinel implements Iterable<Integer>{

    static class Node{
        // 上一个节点指针
        Node prev;
        //值
        int value;
        // 下一个节点指针
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 头部哨兵
     */
    private Node head;
    /**
     * 尾部哨兵
     */
    private Node tail;

    public DoubleLinkedListSentinel() {
        head = new Node(null,666,null);
        tail = new Node(null,888,null);
        head.next = tail;
        tail.prev = head;
    }


    public void insert(int index,int value){
        Node prev = findNode(index - 1);
        if(prev == null){
            throw new IllegalArgumentException(String.format("index [%d] 不合法",index));
        }
        Node next = prev.next;

        Node inserted = new Node(prev,value,next);
        prev.next = inserted;
        next.prev = inserted;

    }

    /**
     * 删除第一个元素
     */
    public void removeFirst(){
        remove(0);
    }
    public void remove(int index){
        Node prev = findNode(index - 1);
        if(prev == null){
            throw new IllegalArgumentException(String.format("index [%d] 不合法",index));
        }

        Node removed = prev.next;
        // 尾哨兵不应被删除
        if(removed == tail){
            throw new IllegalArgumentException(String.format("index [%d] 不合法",index));
        }
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;

    }

    /**
     * 在尾部添加元素
     */
    public void addLast(int value){
        Node last = tail.prev;
        Node added = new Node(last,value,tail);
        last.next = added;
        tail.prev = added;
    }

    public void removeLast(){
        Node removed = tail.prev;
        if(removed == head){
            // 待删除节点为头哨兵,不允许删除
            throw new IllegalArgumentException(String.format("待删除的索引不正确,不允许删除头哨兵"));
        }
        Node prev = removed.prev;
        prev.next = tail;
        tail.prev = prev;
    }

    /**
     * 在头部插入第一个元素
     * @param value
     */
    public void addFirst(int value){
        insert(0,value);
    }



    /**
     * 根据索引位置查找节点
     * @param index
     * @return
     */
    private Node findNode(int index){
        // 头哨兵为-1
        int i=-1;
        for (Node p = head;p !=tail;p = p.next,i++) {
            if(i== index){
                return p;
            }

        }
        return null;

    }



    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            // 遍历起始指针
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p!=tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

}
